package impl;

import Entity.Notice;
import util.MySQLConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class NoticeOpt {
    /***
     *  增加维修信息
     * @param notice 维修的信息
     */
    public   void add(Notice notice) throws SQLException {
        String strSQL ="insert into notice(id,title,content,date,type) values (?,?,?,?,?)";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setInt(1,notice.getId());
        preparedStatement.setString(2,notice.getTitle());
        preparedStatement.setString(3,notice.getContent());
        preparedStatement.setString(4,notice.getDate());
        preparedStatement.setString(5,notice.getType());
        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("插入失败");
        }else {
            System.out.println("插入成功"+i);
        }

    }


    /***
     *  删除设备功能
     * @param notice 删除设备维修信息信息
     */
    public  void delete(Notice notice) throws SQLException {

        String strSQL =" delete  from  notice where id = ? ";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setInt(1,notice.getId());

        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("更新失败");
        }else {
            System.out.println("更新成功"+i);
        }
    }


    /***
     * 更新数据
     * @param notice 更新维修信息
     */
    public  void update(Notice notice) throws SQLException {
        String strSQL ="update  notice  set title =?,content=?,date=?,type=? where id =?";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setString(1,notice.getTitle());
        preparedStatement.setString(2,notice.getContent());
        preparedStatement.setString(3,notice.getDate());
        preparedStatement.setString(4,notice.getType());
        preparedStatement.setInt(5,notice.getId());

        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("更新失败");
        }else {
            System.out.println("更新成功"+i);
        }

    }

    /***
     * 查询数据
     * @param notice 查询维修信息数据
     */
    public  void search(Notice notice) throws SQLException {

        String strSQL =" select * from notice";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        ResultSet resultSet = preparedStatement.executeQuery(); //执行之后返回的结果
        while(resultSet.next()){ //通过迭代器循环获取所有的值。
            System.out.println(resultSet.getString("id")+"-"+resultSet.getString("title")+"-"+resultSet.getString("content")+"-"+resultSet.getString("date")+"-"+resultSet.getString("type"));
        }
    }
}
